package com.gfxy.modules.common.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gfxy.aspect.DictTranslateService;
import com.gfxy.aspect.PlantformDictAspect;
import com.gfxy.modules.common.entity.GfxyTeacherInfo;
import com.gfxy.modules.common.entity.TotalScore;
import com.gfxy.modules.common.service.IGfxyTeacherInfoService;
import com.gfxy.modules.common.service.ITotalScoreService;
import com.gfxy.modules.common.vo.SummaryOrgTotalScoreVo;
import com.gfxy.modules.common.vo.SummaryTotalScoreVo;
import com.gfxy.modules.common.vo.TotalScoreOrg;
import com.gfxy.modules.common.vo.TotalScoreVo;
import com.gfxy.modules.system.entity.SysDepart;
import com.gfxy.modules.system.service.ISysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.util.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;
import java.util.List;

/**
 * @author: lijixin 2021/11/19 10:42 上午
 */
@Api(tags="总分")
@RestController
@RequestMapping("common/totalScore")
@Slf4j
public class TotalScoreController  {

    @Autowired
    private ITotalScoreService totalScoreService;
    @Autowired
    private DictTranslateService dictTranslateService;

    @Autowired
    private ISysDepartService sysDepartService;


//    @AutoLog(value = "成员列表通过主表ID查询")
//    @ApiOperation(value="成员列表主表ID查询", notes="成员列表-通主表ID查询")
//    @GetMapping(value = "/listByTeacherIdAndDate")
//    public Result<?> queryMemberListByMainId(@RequestParam(name="username",required=true) String username,
//                                             @RequestParam(name="date",required=true) String date) {
//
//        QueryWrapper<TotalScore> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("teacher_id", username).eq("date", date);
//        List<TotalScore> totalScores = totalScoreService.list(queryWrapper);
//        return Result.OK(totalScores);
//    }
//
//    @AutoLog(value = "通过时间和部门，查询每个教师的社会与服务总分")
//    @ApiOperation(value="通过时间和部门，查询每个教师的社会与服务总分", notes="通过时间和部门，查询每个教师的社会与服务总分")
//    @GetMapping(value = "/listEducationAndServiceScoreByOrgCodeAndDate")
//    public Result<?> queryEducationAndServiceScore(@RequestParam(name="date",required=true) String date,@RequestParam(name="orgCode",required=true) String orgCode,
//                                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize){
//        IPage<TotalScoreVo> totalScores = totalScoreService.getEducationAndServiceByDateAndOrgCode(date,orgCode,pageNo,pageSize);
//        return Result.OK(totalScores);
//    }



    @AutoLog(value = "通过时间和部门，查询每个教师的教学建设总分")
    @ApiOperation(value="通过时间和部门，查询每个教师的教学建设总分", notes="通过时间和部门，查询每个教师的教学建设总分")
    @GetMapping(value = "/listSummaryTotalScore")
    public Result<?> listSummaryTotalScore(@RequestParam(name="orgCode",required=true) String orgCode,
                                             @RequestParam(name="date",required=true) String date,
                                             @RequestParam(name="scoreType",required=true) Integer scoreType,
                                             @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                             @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            @RequestParam(name="projectId", required=false) String projectId) {
        if(scoreType == 0){
            //总分
            if(orgCode.equals("A05")){
               List<SummaryOrgTotalScoreVo> totalScoreOrgs = totalScoreService.getOrgAllYearSummaryTotalScoreByDate(date, projectId);
               return Result.OK(totalScoreOrgs);
            } else {
                IPage<SummaryTotalScoreVo> totalScores = totalScoreService.getAllYearSummaryTotalScoreByDateAndOrgCode(date,orgCode,pageNo,pageSize);
                return Result.OK(totalScores);
            }
        } else {
            if(orgCode.equals("A05")){
//                List<TotalScoreOrg> totalScoreOrgs = totalScoreService.getOrgSummaryTotalScoreByDate(date,scoreType, projectId);
//                return Result.OK(totalScoreOrgs);
            } else {
//                IPage<TotalScoreVo> totalScores = totalScoreService.getSummaryTotalScoreByDateAndOrgCode(date,orgCode,scoreType,pageNo,pageSize);
//                return Result.OK(totalScores);
            }
        }
        return null;
    }



    /**
     * 根据时间与部门查询育人与服务总分
     * @param date
     * @param orgCode
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @GetMapping(value = "/getEducationTotalScore")
    Result<?> getEducationTotalScore(@RequestParam(name="date", defaultValue="1") String date,
                                     @RequestParam(name="orgCode", defaultValue="50") String orgCode,
                                     @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize", defaultValue="50") Integer pageSize,
                                     HttpServletRequest req){

        IPage<TotalScoreVo> totalScores = this.totalScoreService.getEducationTotalScore(date,orgCode,pageNo, pageSize);
        Result result =  Result.OK(totalScores);
        dictTranslateService.parseDictText(result);
        return result;
    }


    @RequestMapping(value = "/exportXlsTemplate")
    public ModelAndView exportXls(
            @RequestParam(name="orgCode") String orgCode,
            @RequestParam(name="date") String date,
            HttpServletRequest request)  {
        try{
            List<SummaryTotalScoreVo> totalScores = null;
//            if(orgCode.equals("A05")){
//                totalScores = totalScoreService.getAllSummaryTotalScoreByDateForList(date);
//            } else {
//                totalScores = totalScoreService.getAllSummaryTotalScoreByDateAndOrgCodeForList(date,orgCode);
//            }
            totalScores = totalScoreService.getAllSummaryTotalScoreByDateAndOrgCodeForList(date,orgCode);



            SysDepart depart = sysDepartService.getById(orgCode);
            String orgName = depart.getDepartName();

            Iterator var3 = totalScores.iterator();
            List<JSONObject> items = dictTranslateService.translateDict(var3);




            TeacherTotalScoreExcelView teacherTotalScoreExcelView = SpringContextHolder.getBean(TeacherTotalScoreExcelView.class);
            ModelAndView mv = new ModelAndView(teacherTotalScoreExcelView);
//            mv.addObject(EducationAndServiceExcelView.FILE_NAME,"育人与服务模板");
            if(orgCode.equals("A05")){
                mv.addObject(TotalScoreYearExcelView.TITLE, date + "年 " + " 专职教师绩效考核成绩汇总");
            } else {
                mv.addObject(TotalScoreYearExcelView.TITLE, date + "年 " + orgName + " 专职教师绩效考核成绩汇总");
            }

            mv.addObject(TeacherTotalScoreExcelView.CONTENT, items);
            return mv;
        }catch (Exception e){
            throw e;
        }
    }

}
